package com.ipt.frm.pw;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class PrimerServlet
 */
public class RequestInfo extends ServletMaestro {

	private static final long serialVersionUID = -7222970621860173660L;

	/**
     * @see HttpServlet#HttpServlet()
     */
    public RequestInfo() {
        super();        
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				
		response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<body>");
        out.println("<head>");
        request.setAttribute("title", "Request Info");
        out.println("<title>"+ request.getAttribute("title") +"</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h3>Request Information Example</h3>");        
        out.println("Method: " + request.getMethod() + "<br/>");        
        out.println("Request URI: " + request.getRequestURI() + "<br/>");
        out.println("Protocol: " + request.getProtocol() + "<br/>");
        out.println("PathInfo: " + request.getPathInfo() + "<br/>");
        out.println("Remote Address: " + request.getRemoteAddr() + "<br/>");  
        out.println("Remote Port: " + request.getRemotePort()+ "<br/>");
        out.println("Parametros enviados:  " + request.getParameter("param1") + " " + request.getParameter("param2"));
        //parametros cabecera
        out.println("<h3>Parametros cabecera</h3>"); 
        Enumeration e = request.getHeaderNames();
        while (e.hasMoreElements()) {
            String name = (String)e.nextElement();
            String value = request.getHeader(name);
            out.println(name + " = " + value+ "<br/>");
        }
        
        out.println("<h3>variables de sesion</h3>");
        HttpSession session = request.getSession(true);
        //guardar eb session el atributo recogido por el formulario
        session.setAttribute("user", request.getParameter("nombre"));
        
        // print session info
        Date created = new Date(session.getCreationTime());
        Date accessed = new Date(session.getLastAccessedTime());
        out.println("ID " + session.getId());
        out.println("Created: " + created);
        out.println("Last Accessed: " + accessed);        
        
        out.println("</body>");
        out.println("</html>");
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//puenteado las peticiones POST a doGet
		doGet(request, response);
		/*
		response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<body>");
        out.println("<head>");
        out.println("<title>Request Information Example</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h3>Request Information Example</h3>");        
        out.println("Method: " + request.getMethod() + "<br/>");        
        out.println("Request URI: " + request.getRequestURI()+ "<br/>");
        out.println("Protocol: " + request.getProtocol()+ "<br/>");
        out.println("PathInfo: " + request.getPathInfo()+ "<br/>");
        out.println("Remote Address: " + request.getRemoteAddr()+ "<br/>");
        
        
        //out.println("<br/>" + request.getParameter("nombre"));
        //out.println("<br/>" + request.getParameter("apellido1"));
        //out.println("<br/>" + request.getParameter("tipo"));
        Enumeration paramNames = request.getParameterNames();
        while(paramNames.hasMoreElements())
        {
        	String paramName = (String)paramNames.nextElement();
            out.print("<br/>" + paramName + "\n");
        }
        
        out.println("</body>");
        out.println("</html>");
		*/
	}

}
